package com.aliyun.heiyu.powermaster.server.strategy.analyzer;

import com.aliyun.heiyu.powermaster.proto.StrategyAction;
import com.aliyun.heiyu.powermaster.proto.StrategyDetail;
import com.aliyun.heiyu.powermaster.server.MasterConstant;
import com.aliyun.heiyu.powermaster.server.manager.StrategyManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AnalyzerContext {
    private static Logger logger = LoggerFactory.getLogger(AnalyzerContext.class);

    private StrategyManager strategyManager;
    private List<StrategyAnalyzer> analyzerList = new ArrayList<>();

    public AnalyzerContext() {
        this.strategyManager = StrategyManager.getInstance();
        analyzerList.add(new CappingStrategyAnalyzer());
        analyzerList.add(new AICappingStrategyAnalyzer());
        analyzerList.add(new AICstateStrategyAnalyzer());
        analyzerList.add(new AIDvfsStrategyAnalyzer());
        analyzerList.add(new AIPowerOffStrategyAnalyzer());
    }

    /**
     * 收集、分析（ActionName字段有转变）、汇总
     */
    public Map<String, Map<String, StrategyAction>> getAnalyzeType2SN2Action() {
        //收集
        List<StrategyDetail> strategies = strategyManager.getAllStrategiesOfOn1();
        //转变actionName-->analyzeType
        List<StrategyDetail> strategyList = convertStrategyList(strategies);
        //分析、汇总
        Map<String, Map<String, StrategyAction>> analyzeType2SN2Action = new HashMap<>();
        for (StrategyAnalyzer analyzer : analyzerList) {
            String analyzeType = analyzer.getAnalyzeType();
            Map<String, StrategyAction> sn2Action = analyzer.getChangedSN2Action(strategyList);//进行分析
            if (sn2Action.size() > 0) {
                analyzeType2SN2Action.put(analyzeType, sn2Action);
            }
        }
        return analyzeType2SN2Action;
    }

    //todo 考虑如何去掉这一逻辑
    private List<StrategyDetail> convertStrategyList(List<StrategyDetail> strategies) {
        List<StrategyDetail> strategyList = new ArrayList<>();
        for (StrategyDetail strategyDetail : strategies) {
            int type = strategyDetail.getType();
            StrategyDetail.Builder builder1 = StrategyDetail.newBuilder().mergeFrom(strategyDetail);
            if (1 == type) {
                List<StrategyAction> actions = builder1.getStrategyActionsList();
                StrategyAction AIAction = null;
                if(actions != null && actions.size() !=0){
                    AIAction = actions.get(0);
                }
                builder1.clearStrategyActions();
                StrategyAction.Builder action1 = StrategyAction.newBuilder();
                action1.setActionName(MasterConstant.AI_DVFS);
                builder1.addStrategyActions(action1.build());
                StrategyAction.Builder action2 = StrategyAction.newBuilder();
                action2.setActionName(MasterConstant.AI_CSTATE);
                builder1.addStrategyActions(action2.build());
                StrategyAction.Builder action3 = StrategyAction.newBuilder();
                action3.setActionName(MasterConstant.AI_POWEROFF);
                builder1.addStrategyActions(action3.build());
                StrategyAction.Builder action4 = StrategyAction.newBuilder();
                action4.setActionName(MasterConstant.AI_CAPPING);
                if(AIAction != null){
                    action4.setCapTo(AIAction.getCapTo());
                    action4.setFmin(AIAction.getFmin());
                    action4.setCapType(AIAction.getCapType());
                }
                builder1.addStrategyActions(action4.build());
            }
            strategyList.add(builder1.build());
        }

        return strategyList;
    }

}
